﻿package game 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import game.events.EventData;
	import gs.TweenMax;
	/**
	 * ...
	 * @author ... Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class PageControl extends MovieClip
	{
		public var firstPage:MovieClip;
		public var prePage:MovieClip;
		public var maskPage:MovieClip;
		public var nextPage:MovieClip;
		public var lastPage:MovieClip;
		public var pageList:MovieClip;
		
		private var numPage:uint;
		private var firstNum:uint = 0;
		private var lastNum:uint = 0;
		private var currentPage:uint;
		private var posx:Number;
		private var disPage:uint = 4;
		private var numShowPage:uint = 5;
		private var durationMove:Number = 0.7;
		public function PageControl() 
		{
			firstPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			firstPage.buttonMode = true;
			prePage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			prePage.buttonMode = true;
			nextPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			nextPage.buttonMode = true;
			lastPage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHdl);
			lastPage.buttonMode = true;
			posx = this.pageList.x;
		}
		
		private function mouseDownHdl(e:MouseEvent):void 
		{
			outEvent(this.pageList, this.numPage);
			var event:EventData = new EventData(EventData.GET_RECENT_BET, true);
			setStatusPage(this.pageList, currentPage, "out");
			if (e.currentTarget == this.firstPage  && this.currentPage != 1) {
				if (this.lastNum > numShowPage) {
					TweenMax.to(this.pageList, durationMove, { x:this.posx} );
				}
				this.firstNum = 1;
				this.lastNum = this.firstNum + disPage;
				
				this.currentPage = 1;
				event.ms = this.currentPage.toString();	
				dispatchEvent(event);
			}
			else if (e.currentTarget == this.lastPage && this.currentPage != this.numPage) {
				if(this.firstNum <(this.numPage - disPage)){
					TweenMax.to(this.pageList, durationMove, { x: (this.posx - ((this.numPage - numShowPage) * 30)) } );
					
				}
				this.lastNum = this.numPage;
				this.firstNum = this.lastNum - disPage;
				this.currentPage = this.numPage;
				event.ms = this.currentPage.toString();	
				dispatchEvent(event);
				
			}else if (e.currentTarget == this.prePage) {
				if (currentPage > 1) {
					this.currentPage = this.currentPage - 1;
					event.ms = this.currentPage.toString();	
					dispatchEvent(event);
					if (this.currentPage ==  this.firstNum)
					{
						preAnimation();
					}
				}
			}else if (e.currentTarget == this.nextPage) {
				if (currentPage < this.numPage) {
					this.currentPage = this.currentPage + 1;
					event.ms = this.currentPage.toString();	
					dispatchEvent(event);
					if (this.currentPage == this.lastNum) {
						nextAnimation();
					}
				}
			}
			setStatusPage(this.pageList, currentPage, "down");
		
		}
		
		private function removePages(pageList:MovieClip):void {
			var numberChid:int = pageList.numChildren;
			if (numberChid> 0)
			{
				for (var j:int = 0; j < numberChid; j++ ) 
				{
					pageList.removeChildAt(0);
				}
			}
		}
		
		public function addPageToPageList(pageList:MovieClip, numPage:uint):void {
			
			this.numPage = numPage;
			this.firstNum = 1;
			this.lastNum = firstNum + disPage;
			this.currentPage = 1;
			removePages(pageList);
			var dis:int = 30;
			var xPos:int = prePage.x + prePage.width + 3;
			for (var i:int = 1; i <= numPage; i++ )
			{
				var page:MovieClip = MovieClip(new PageNum());
				page.buttonMode = true;
				page.name = i.toString();
				page.x = xPos;
				xPos += dis;
				page.txtPageTf.text = i;
				page.txtPageTf.mouseEnabled = false;
				page.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownPageHdl);
				pageList.addChild(page);
				if (i == 1)
					page.gotoAndStop("down");
			}
		}
		
		private function mouseDownPageHdl(e:MouseEvent):void 
		{
			outEvent(this.pageList, this.numPage);
			e.currentTarget.gotoAndStop("down");
			var event:EventData = new EventData(EventData.GET_RECENT_BET, true);
			event.ms = e.currentTarget.name;
			dispatchEvent(event);
			currentPage = uint(e.currentTarget.name);
			if (this.currentPage == this.lastNum && this.lastNum < this.numPage)
				nextAnimation();
			if (this.currentPage == this.firstNum && this.firstNum > 1)
				preAnimation();
		}
		private function outEvent(pageList:MovieClip,numPage:uint) {
			for (var i:int = 1; i <= numPage; i++) {
				var temp:DisplayObject = pageList.getChildByName(i.toString());
				MovieClip(temp).gotoAndStop("out");
			}
		}
		
		private function setStatusPage(pageList:MovieClip, numPage:uint, frame:String):void {
			var temp:DisplayObject = pageList.getChildByName(numPage.toString());
			MovieClip(temp).gotoAndStop(frame);
		}
		
		private function nextAnimation():void {
			this.lastNum += 2;
			if (this.currentPage == (this.numPage - 1))
			{
				TweenMax.to(this.pageList, durationMove, { x:this.pageList.x - 30 } );
				this.firstNum = this.numPage - disPage;
				this.lastNum = this.numPage;
			}else if(this.lastNum <= this.numPage) {
				TweenMax.to(this.pageList, durationMove, { x:this.pageList.x - 60 } ); 
				this.firstNum = this.lastNum - disPage;
			}
			
		}
		
		private function preAnimation():void {
			this.firstNum -= 2;
			if (this.currentPage == 2) {
				TweenMax.to(this.pageList, durationMove, { x:this.pageList.x + 30 } );
				this.firstNum = 1;
				this.lastNum  =  this.firstNum + disPage;
			} else if ((this.currentPage - 2) >= 0) {
				TweenMax.to(this.pageList, durationMove, { x:this.pageList.x + 60 } );
				this.lastNum = this.firstNum + disPage;
			}
			
		}
		
	}
}